package com.situ.stmall.admin.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.situ.stmall.admin.entity.APIResult;
import com.situ.stmall.admin.entity.GoodsSort;
import com.situ.stmall.admin.service.GoodsSortService;

@RestController
@RequestMapping("/api/goodssorts")
@CrossOrigin(origins = {"*"})
public class GoodsSortController {

	 @Autowired
	 private GoodsSortService goodsSortService;
	 
	 /**
	  * 添加
	  * @param goodsSort
	  * @return
	  */
	 @PostMapping
	 public APIResult add(@RequestBody GoodsSort goodsSort) {
		goodsSort = goodsSortService.add(goodsSort);
		return APIResult.created(goodsSort);
	}
	 /**
	  * 删除
	  * @param id
	  * @return
	  */
	 @DeleteMapping
	 public APIResult remove(Integer id) {
		 GoodsSort goodsSort = goodsSortService.remove(id);
		 		
		 if (goodsSort != null) {
			 return APIResult.ok(goodsSort);
		} else {
			return APIResult.notFound("对象不存在");
		}		
	 }
	 
	 /**
	  * 修改
	  * @param goodsSort
	  * @return
	  */
	 @PutMapping
	 public APIResult edit(@RequestBody GoodsSort goodsSort) {
		 goodsSort = goodsSortService.edit(goodsSort);
		 if (goodsSort != null) {
			return APIResult.created(goodsSort);
		} else {
			return APIResult.notFound("要修改的对象不存在");
		}		
	 }
	 
	 /**
	  * 查询
	  * @param page
	  * @param limit
	  * @param goodsSort查询条件
	  * 	name 模糊查询
	  *		parent传0 查询所有分类
	  *		parent 传null查询一级分类
	  *		status 传null查询所有分类
	  * @return
	  */
	 @GetMapping
	 public APIResult get(Integer page, Integer limit, GoodsSort goodsSort) {
		 PageInfo pageInfo = goodsSortService.getByPage(page,limit,goodsSort);
		 
		 if (pageInfo == null || pageInfo.getTotal() == 0) {
			return APIResult.notFound("未查询到任何数据");
		} else {
			return APIResult.ok(pageInfo);
		}
	 }
}
